import time
import traceback

import api
from live import get_live_list

livemap = {}
n = 1


def start():
    global n
    while True:
        get_live_list(livemap)
        pstr = ""
        for l in livemap.values():
            pstr += l.name + " "
        print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), "livemap: ", pstr)
        # 没有直播，慢慢延长到10分钟检查一次
        if len(livemap) > 0:
            n = 1
        elif len(livemap) == 0 and n < 6:
            n += 1
        api.wait_random_time(100 * n)


if __name__ == '__main__':
    while True:
        try:
            start()
        except Exception as e:
            api.logger.info(traceback.format_exc())
            print(e.__str__())
            print(traceback.format_exc())

            livemap.clear()
            api.wait_random_time(20)
            # while True:
            #     if api.check_network():
            #         break
            #     else:
            #         api.wait_random_time(20)